1272B - Snow Walking Robot - CodeForces Solution


constructive algorithms greedy implementation *1200

Please click on ads to support us..

Python Code:

for _ in[0]*int(input()):
 u,v,*x=map(input().count,'LRUD');a=min(u,v);b=min(x)
 if b<1:a=a and 1
 elif a<1:b=1
 print(2*(a+b),'L'*a+'U'*b+'R'*a+'D'*b)

C++ Code:

/***from dust i have come, dust i will be***/

#include<bits/stdc++.h>

typedef long long int ll;
typedef unsigned long long int ull;

#define dbg printf("in\n")
#define nl printf("\n");
#define N 210101
#define inf 1e9

#define sf(n) scanf("%d", &n)
#define sff(n,m) scanf("%d%d",&n,&m)
#define sfl(n) scanf("%I64d", &n)
#define sffl(n,m) scanf("%I64d%I64d",&n,&m)

#define pf(n) printf("%d",n)
#define pff(n,m) printf("%d %d",n,m)
#define pffl(n,m) printf("%I64d %I64d",n,m)
#define pfl(n) printf("%I64d",n)
#define pfs(s) printf("%s",s)

#define pb push_back
#define pp pair<int, int>

using namespace std;

int main()
{
    // freopen("in.txt", "r", stdin);

    int i, j, k;
    int n, m, t;
    int u, d, l, r;
    string s;

    sf(t);
    while(t--)
    {
        cin >> s;

        n = s.length();
        u = d = l = r = 0;

        for(i = 0; i < n; i++)
        {
            if(s[i] == 'U')
                u++;
            else if(s[i] == 'D')
                d++;
            else if(s[i] == 'L')
                l++;
            else
                r++;
        }

        m = min(u, d);
        u = d = m;

        m = min(l, r);
        l = r = m;

        string ans = "";

        // after some u / d there must be some l / r and vice versa
        if(u && l && r && d)
        {
            while(u--)
                ans.pb('U');
            while(r--)
                ans.pb('R');
            while(d--)
                ans.pb('D');
            while(l--)
                ans.pb('L');
        }

        else
        {
            if(r && l)
                ans = "LR";
            else if(u && d)
                ans = "UD";
        }

        pf(ans.length());
        nl;
        cout << ans << endl;
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements